{# Copyright The IETF Trust 2015, All Rights Reserved #}
{% load origin %}
{% load static %}
{% origin %}
{% if settings.DEBUG %}
    {% if sql_debug %}
        {% load debug_filters %}
        {{ sql_queries|length }} queries ({{ sql_queries|timesum }}s)
        {% if sql_queries|length != 0 %}
            <a class="btn btn-primary btn-sm"
               onclick="$('#debug-query-table').toggleClass('d-none');">Show</a>
        {% endif %}
        <div id="debug-query-table" class="d-none">
            <div class="text-start">
                <table class="table table-sm tablesorter">
                    <thead>
                        <tr>
                            <th scope="col" data-sort="num">#</th>
                            <th scope="col" data-sort="query">SQL</th>
                            <th scope="col" data-sort="count">Count</th>
                            <th scope="col" data-sort="where">WHERE</th>
                            <th scope="col" data-sort="view-num">View/Templ.</th>
                            <th scope="col" data-sort="time-num">Time</th>
                            <th scope="col" data-sort="acc-num">Acc.</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% with sql_queries|annotate_sql_queries as sql_query_info %}
                            {% for query in sql_query_info %}
                                <tr class="sql">
                                    <td>{{ forloop.counter }}</td>
                                    <td>{{ query.sql|expand_comma|escape }}</td>
                                    <td>{{ query.count }}</td>
                                    <td>{{ query.where }}</td>
                                    <td>
                                        {{ query.loc }}
                                        {% if query.origin %}
                                            <button type="button"
                                                    class="btn btn-primary btn-sm"
                                                    data-bs-toggle="modal"
                                                    data-bs-target="#modal-{{ forloop.counter }}">
                                                Origin
                                            </button>
                                            <div class="modal fade"
                                                 id="modal-{{ forloop.counter }}"
                                                 tabindex="-1"
                                                 role="dialog"
                                                 aria-labelledby="modal-title-{{ forloop.counter }}">
                                                <div class="modal-dialog modal-max" role="document">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <button type="button"
                                                                    class="close"
                                                                    data-bs-dismiss="modal"
                                                                    aria-label="Close">
                                                                <span aria-hidden="true">&times;</span>
                                                            </button>
                                                            <h4 class="modal-title" id="modal-title-{{ forloop.counter }}">QuerySet Origin for Query #{{ forloop.counter }}</h4>
                                                        </div>
                                                        <div class="modal-body">
                                                            <table class="table table-sm">
                                                                <thead>
                                                                    <tr>
                                                                        <th scope="col">File (line)</th>
                                                                        <th scope="col">Method</th>
                                                                        <th scope="col">Code</th>
                                                                    </tr>
                                                                </thead>
                                                                <tbody>
                                                                    {% for origin in query.origin %}
                                                                        <tr class="origin">
                                                                            <td>{{ origin.1 }}({{ origin.2 }})</td>
                                                                            <td>{{ origin.6 }}()</td>
                                                                            <td class="code">
                                                                                {% for l in origin.4 %}
                                                                                    <table>
                                                                                        <tr>
                                                                                            <td class="{% if forloop.counter0 == origin.5 %} current{% endif %}">{{ l }}</td>
                                                                                        </tr>
                                                                                    </table>
                                                                                {% endfor %}
                                                                            </td>
                                                                        </tr>
                                                                    {% endfor %}
                                                                </tbody>
                                                            </table>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Close</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        {% endif %}
                                    </td>
                                    <td>{{ query.time }}</td>
                                    <td>{{ query.time_accum }}</td>
                                </tr>
                            {% endfor %}
                        {% endwith %}
                    </tbody>
                </table>
            </div>
        </div>
    {% else %}
        <small class="text-center text-body-secondary">
            Add <code>ietf.context_processors.sql_debug</code> to <code>settings.DEV_TEMPLATE_CONTEXT_PROCESSORS</code> to turn on the SQL statement table.
        </small>
    {% endif %}
{% endif %}
